﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Delparment;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.Delparment
{
    public class GetDelparmentHandler : IRequestHandler<GetDelparmentCommon, ApiResult<List<DelparmentDTO>>>
    {
        private readonly IBaseRepository<DepartmentModel> deparmentRepo;

        public GetDelparmentHandler(IBaseRepository<DepartmentModel> deparmentRepo)
        {
            this.deparmentRepo = deparmentRepo;
        }

        public Task<ApiResult<List<DelparmentDTO>>> Handle(GetDelparmentCommon request, CancellationToken cancellationToken)
        {
            ApiResult<List<DelparmentDTO>> res=new ApiResult<List<DelparmentDTO>>();

            var info = deparmentRepo.GetAll().Select(x=>new DelparmentDTO()
            {
                DepartmentId = x.DepartmentId,
                DepartmentName = x.DepartmentName,
            }).ToList();
            res.Code = ApiEnum.Success;
            res.Data = info;
            res.Message = "部门下拉框查询成功";
            return Task.FromResult(res);


        }
    }
}
